﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using UniMana.Utils.Data;
using UniMana.Data;
using UniMana.Utils.Controls;
using UniMana.Utils.Helper;

namespace UniMana.Win.Training
{
    public partial class FrmScore_StatusInput : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_Catalogue = new DBMain();
        private bds m_bds = new bds();
        private DBMain m_Score = new DBMain();
        public FrmScore_StatusInput()
        {
            InitializeComponent();
            
            UniMana.Data.SysUtils.SetRuleControl(this.Controls, this.Name);
            LoadData();
            InitControlEvent();
        }
        #region [Ultis]
        public void LoadData()
        {
            lueCatalogue_Term.GetData(m_Catalogue.m_DCReadOnly.Catalogue_Terms);
            lueCatalogue_SchoolYear.GetData(m_Catalogue.m_DCReadOnly.Catalogue_SchoolYears);
            lueTraining_ScoreType.GetData(m_Catalogue.m_DCReadOnly.Training_ScoreTypes);
        }
        #endregion
        #region[Events]
        public void InitControlEvent()
        {
            //xucBar2.btnAddNew_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnAddNew_Click);
            //xucBar1.btnSave_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnSave_ItemClick);
            //xucBar1.btnRefresh_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnRefresh_ItemClick);
            xucBar1.btnExport_Click += new UniMana.Utils.Controls.xucBar.ButtonClickHandler(this.btnExportExcel_ItemClick);
            xucBar1.btnPrint_Click += new UniMana.Utils.Controls.xucBar.ButtonClickHandler(this.btnPrint_ItemClick);
            //xucBar1.btnEdit_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnEdit_Click);
            //xucBar1.btnCancel_Click += new UniMana.Controls.xucBar.ButtonClickHandler(btnCancel_Click);
            //xucBar1.btnImport_Click += new UniMana.Controls.xucBar.ButtonClickHandler(btnImportExcel_ItemClick);
        }
        //Buttons
        private void btnExportExcel_ItemClick(object sender, ItemClickEventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.FileName = "Tình trạng nhập điểm của các lớp học phần_"+ DateTime.Now.Date.ToShortDateString().Replace('/', '_');
            saveFile.Title = "Xuất ra tệp tin";
            saveFile.DefaultExt = "xls";
            saveFile.Filter = "Excel 2003 (*.xls)|";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                grvSubjectClass.ExportToXls(saveFile.FileName);
            }
        }
        private void btnPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            printableComponentLink1.ClearDocument();
            printableComponentLink1.ShowPreviewDialog();
        }
        #endregion
        //GridView
        private void grvSubjectClass_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }
        private void grvSubjectClass_DoubleClick(object sender, EventArgs e)
        {
            //|EDIT
            #region [Tân cũ]
            /*
            if (grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]) != null)
            {
                if (grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]).ToString() != "")
                {
                    int subjectClassKey =Convert.ToInt32( grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]).ToString());
                    xucBar1.dlg = new DevExpress.Utils.WaitDialogForm();
                    foreach (XtraTabPage tp in ((XtraTabControl)(((XtraTabPage)this.Parent)).Parent).TabPages)
                    {
                        if (tp.Text == "Nhập điểm")
                        {
                            tp.Controls.Clear();
                            FrmScore_Input score = new FrmScore_Input(subjectClassKey, FrmScore_Input.eFuncTions.View);
                            score.TopLevel = false;
                            tp.Controls.Add(score);
                            score.Dock = DockStyle.Fill;
                            score.Show();
                            xucBar1.dlg.Dispose();
                            ((XtraTabControl)(((XtraTabPage)this.Parent)).Parent).SelectedTabPage = tp;
                            return;
                            //((XtraTabControl)(((XtraTabPage)this.Parent)).Parent).TabPages.Remove(tp);
                        }
                    }
                    TabShowFrm.ShowTabbedMdi(new FrmScore_Input(subjectClassKey, FrmScore_Input.eFuncTions.View), (XtraTabControl)(((XtraTabPage)this.Parent)).Parent);
                    xucBar1.dlg.Dispose();
                }
            }
             */
            #endregion
            /*
            DXMouseEventArgs dx = (DXMouseEventArgs)e;
            #region [Hòa 01/12/2012]
            if (dx.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]) != null)
                {
                    if (grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]).ToString() != "")
                    {
                        int subjectClassKey = Convert.ToInt32(grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]).ToString());
                        xucBar1.dlg = new DevExpress.Utils.WaitDialogForm();
                        foreach (XtraTabPage tp in ((XtraTabControl)this.Parent.Parent.Parent.Parent.Parent).TabPages)
                        {
                            if (tp.Text == "Nhập điểm")
                            {
                                tp.Controls.Clear();
                                FrmScore_Input score = new FrmScore_Input(subjectClassKey, FrmScore_Input.eFuncTions.View);
                                score.TopLevel = false;
                                tp.Controls.Add(score);
                                score.Dock = DockStyle.Fill;
                                score.Show();
                                xucBar1.dlg.Dispose();
                                ((XtraTabControl)(this.Parent.Parent.Parent.Parent.Parent)).SelectedTabPage = tp;
                                return;
                                //((XtraTabControl)(((XtraTabPage)this.Parent)).Parent).TabPages.Remove(tp);
                            }
                        }
                        TabShowFrm.ShowTabbedMdi(new FrmScore_Input(subjectClassKey, FrmScore_Input.eFuncTions.View), ((XtraTabControl)this.Parent.Parent.Parent.Parent.Parent));
                        xucBar1.dlg.Dispose();
                    }
                }
            }
            #endregion
             */
        }
        //LookupEdit
        private void lueCatalogue_SchoolYear_EditValueChanged(object sender, EventArgs e)
        {
            getSubjectClass();
        }
        private void lueCatalogue_Term_EditValueChanged(object sender, EventArgs e)
        {
            getSubjectClass();
        }
        private void lueTraining_ScoreType_EditValueChanged(object sender, EventArgs e)
        {
            getSubjectClass();
        }
        //Methods
        private void getSubjectClass()
        {
            if (lueCatalogue_Term.EditValue != null && lueCatalogue_SchoolYear.EditValue != null && lueTraining_ScoreType.EditValue !=null)
            {
                m_bds = new bds();
                m_bds.DataSource = m_Score.m_DCReadOnly.Training_Subject_Classes.GetSubjectClassList(m_Score.m_DCReadOnly.Training_SubjectClass_ScoreTypes,
                                                        m_Score.m_DCReadOnly.Training_ScoreTypes, m_Score.m_DCReadOnly.Personnel_Personnels,
                                lueCatalogue_Term.GetIntValue(), lueCatalogue_SchoolYear.GetIntValue(), lueTraining_ScoreType.GetIntValue());
                gctSubjectClass.DataSource = m_bds;
                lbcCheck.Visible = true;
                lbcInput.Visible = true;
                if (grvSubjectClass.RowCount > 0)
                {
                    int status = 0;
                    int check = 0;
                    for (int i = 0; i < grvSubjectClass.RowCount; i++)
                    {
                        if (grvSubjectClass.GetRowCellValue(i, grvSubjectClass.Columns["Lock"]).ToString() == "Xong")
                        {
                            status++;
                        }
                        if (grvSubjectClass.GetRowCellValue(i, grvSubjectClass.Columns["Check"]).ToString() == "Xong")
                        {
                            check++;
                        }
                    }
                    #region [old]
                    //DataTable dt = m_Score.m_DCReadOnly.Training_Subject_Classes.GetList(lueCatalogue_Term.GetIntValue(), lueCatalogue_SchoolYear.GetIntValue()).ToDataTable();
                    //for (int i = 0; i < dt.Rows.Count; i++)
                    //{
                    //    if (dt.Rows[i]["CheckInputScore"].ToString() == "True")
                    //    {
                    //        check++;
                    //    }
                    //    if (dt.Rows[i]["CheckInputScore"].ToString() == "True")
                    //    {
                    //        status++;
                    //    }
                    //}
                   
                    //lbcInput.Text = "Số lớp đã nhập điểm xong: " + status.ToString() + "/" + grvSubjectClass.RowCount.ToString();
                    //lbcCheck.Text = "Số lớp đã kiểm tra xong:  " + check.ToString() + "/" + grvSubjectClass.RowCount.ToString();
                    #endregion
                }
                else
                {
                    lbcCheck.Visible = false;
                    lbcInput.Visible = false;
                }
            }
        }
    }
}